﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLApp
{
    public partial class Form1 : Form
    {
        private Point ePosition;
        private Point Position;


        public Form1()
        {
            InitializeComponent();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否退出系统？", "系统提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            linkLabel1.Enabled = false;      //在申请账号功能没有实装之前请勿删除此行
            btnSetConnect.Visible = false;   //在远程连接功能没有实装之前请勿删除此行
        }

        private void lblDragArea_MouseDown(object sender, MouseEventArgs e)
        {
            ePosition = new Point(e.X,e.Y);
        }

        private void lblDragArea_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Position = Control.MousePosition;
                Position.Offset(-(ePosition.X),-(ePosition.Y));
                this.DesktopLocation = Position;
            }
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            ePosition = new Point(e.X, e.Y);
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Position = Control.MousePosition;
                Position.Offset(-(ePosition.X + 53), -(ePosition.Y + 9));
                this.DesktopLocation = Position;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text == string.Empty || txtPwd.Text == string.Empty)
            {
                MessageBox.Show("请确认您的账号密码输入！", "帐密不能为空", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPwd.Text = string.Empty;
            }
            else
            {
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = "Server=Localhost;Database=CQP;User=edward;Password=mike90303;";
                bool isConnectSuccess = true;
                try
                {
                    conn.Open();
                }
                catch (MySqlException ex)
                {
                    isConnectSuccess = false;
                    MessageBox.Show("连接失效！请重新启动应用程序或者联系管理员！\n" + ex.ToString(), "连接失败！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //这里是处理配置连接的语句入口，暂时先预留入口

                }
                finally
                {
                    if (isConnectSuccess)
                    {
                        MySqlCommand comGetKey = conn.CreateCommand();
                        comGetKey.CommandText = "select `key` from `user` where `username`='" + txtUser.Text + "'";
                        MySqlDataReader dr = comGetKey.ExecuteReader();
                        if (!dr.HasRows)
                        {
                            MessageBox.Show("请确认您的账号密码输入！", "错误的用户名/密码", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtPwd.Text = string.Empty;
                        }
                        else
                        {
                            dr.Read();
                            string key = dr["key"].ToString();
                            MessageBox.Show(Encoding.UTF8.GetByteCount(key).ToString());
                            
                            //byte[] toEncryptArray = Encoding.UTF8.GetBytes(txtPwd.Text);
                            RijndaelManaged rm = new RijndaelManaged();
                            rm.KeySize = 256;
                            rm.GenerateKey();
                            MessageBox.Show(Convert.ToBase64String(rm.Key));
                            //rm.Key = Encoding.UTF8.GetBytes(key);
                            //rm.Padding = PaddingMode.PKCS7;
                            //rm.Mode = CipherMode.CBC;
                            
                        }
                    }
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                    conn.Dispose();
                }
            }
        }
    }
}
